package com.jt.redis;

import redis.clients.jedis.Jedis;

import java.util.Set;

public class VoteDemo01 {
    static final String IP="192.168.126.129";
    static final int PORT=6379;
    public static void main(String[] args) {
        String activityId="10001";
        String user1 = "301";
        String user2 = "302";
        Boolean flag = isVote(activityId, user1);
        System.out.println("flag="+flag);
        doVote(activityId, user2);
        doVote(activityId, user1);
        Long aLong = doGetVotes(activityId);
        System.out.println(activityId+"的总投票:"+aLong);
        Set<String> users = doGetUsers(activityId);
        System.out.println("参与"+activityId+"活动的投票用户:"+users);

    }
    public static void doVote(String activityId,String userId){
        Jedis jedis = new Jedis(IP, PORT);
        jedis.sadd(activityId, userId);
        jedis.close();

    }
    public static Long doGetVotes(String activityId){
        Jedis jedis = new Jedis(IP, PORT);
        Long count = jedis.scard(activityId);
        jedis.close();
        return count;

    }
    public static Set<String> doGetUsers(String activityId){
        Jedis jedis = new Jedis(IP, PORT);
        Set<String> smembers = jedis.smembers(activityId);
        jedis.close();
        return smembers;

    }
    public static Boolean isVote(String activityId, String userId){
        Jedis jedis = new Jedis(IP,PORT);
        Boolean flag = jedis.sismember(activityId, userId);
        jedis.close();
        return flag;

    }


}
